﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PsiIndexerGUI
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainView : Window
  {
    private string landscapeName = "landscape";
    private string portraitName = "portrait";

    public MainView()
    {
      this.InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      ColumnDefinition lcd = this.mainGrid.ColumnDefinitions.FirstOrDefault(rd => rd.Name == this.landscapeName);
      if (lcd != null) {
        this.mainGrid.ColumnDefinitions.Remove(lcd);
      }

      var ui = TaskScheduler.FromCurrentSynchronizationContext();
      Task<MainViewModel>.Factory.StartNew(() => new MainViewModel(PsiIndexer.PsiIndexer.Create(@"c:\Users\skraemer\PsiData\profiles\default\history\", true, false, this.showInfo)))
        .ContinueWith(task => this.DataContext = task.Result, ui)
        .ContinueWith(task => this.busyOverlay.Visibility = Visibility.Collapsed, ui)
        .ContinueWith(task => this.searchTerm.Focus(), ui);
    }

    private void showInfo(string obj)
    {
      if (!this.Dispatcher.CheckAccess()) {
        this.Dispatcher.Invoke(new Action<string>(this.showInfo), obj);
      }
      else {
        this.statusInfo.Text = obj;
      }
    }

    private void Size_Changed(object sender, SizeChangedEventArgs e)
    {
      bool isLandscape = e.NewSize.Height < e.NewSize.Width;
      bool wasLandscape = e.PreviousSize.Height < e.PreviousSize.Width;
      if (isLandscape != wasLandscape) {
        if (isLandscape) {
          this.surroundingChats.SetValue(Grid.RowProperty, 0);
          this.surroundingChats.SetValue(Grid.ColumnProperty, 1);
          this.surroundingChats.SetValue(Grid.RowSpanProperty, 2);
          this.surroundingChats.Margin = new Thickness(5, 0, 0, 0);

          RowDefinition prd = this.mainGrid.RowDefinitions.FirstOrDefault(rd => rd.Name == this.portraitName);
          if (prd != null) {
            this.mainGrid.RowDefinitions.Remove(prd);
          }
          this.mainGrid.ColumnDefinitions.Add(new ColumnDefinition {Name = this.landscapeName, Width = new GridLength(2, GridUnitType.Star)});
        }
        else {
          this.surroundingChats.SetValue(Grid.RowProperty, 2);
          this.surroundingChats.SetValue(Grid.ColumnProperty, 0);
          this.surroundingChats.SetValue(Grid.RowSpanProperty, 1);
          this.surroundingChats.Margin = new Thickness(0,5,0,0);

          ColumnDefinition lcd = this.mainGrid.ColumnDefinitions.FirstOrDefault(rd => rd.Name == this.landscapeName);
          if (lcd != null) {
            this.mainGrid.ColumnDefinitions.Remove(lcd);
          }
          this.mainGrid.RowDefinitions.Add(new RowDefinition {Name = this.portraitName});
        }
      }
    }
  }
}